package com.jingdianjichi.subject.domain.handler.subject;

import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.common.enums.IsDeletedFlageEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.convert.JudgeSubjectConverter;
import com.jingdianjichi.subject.domain.convert.MultipleSubjectConverter;
import com.jingdianjichi.subject.domain.convert.RadioSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBo;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBo;
import com.jingdianjichi.subject.infra.basic.entity.SubjectJudge;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 多选题目的策略类
 */
@Component
public class MultipleTypeHandler implements SubjectTypeHandler {
    @Resource
    private SubjectMultipleService subjectMultipleService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.MULTIPLE;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        //多选题目的插入
        Preconditions.checkNotNull(subjectInfoBo.getOptionList(), "答案选项为空");
        List<SubjectMultiple> subjectMultipleList = new LinkedList<>();
        subjectInfoBo.getOptionList().forEach(option -> {
            //把option 转化为subjectMultiple
            SubjectMultiple subjectMultiple = MultipleSubjectConverter.INSTANCE
                    .convertBoToEntity(option);
            //注入属性
            subjectMultiple.setSubjectId(subjectInfoBo.getId());
            subjectMultiple.setOptionType(option.getOptionType());
            subjectMultiple.setOptionContent(option.getOptionContent());
            subjectMultiple.setIsCorrect(option.getIsCorrect());
            subjectMultiple.setIsDeleted(IsDeletedFlageEnum.UN_DELETE.getCode());
            subjectMultipleList.add(subjectMultiple);
        });
        subjectMultipleService.batchInsert(subjectMultipleList);
    }

    @Override
    public SubjectOptionBo queue(Long subjectId) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(subjectId);
        subjectMultiple.setIsDeleted(IsDeletedFlageEnum.UN_DELETE.getCode());
        List<SubjectMultiple> multipleResult=   subjectMultipleService.queryByCondition(subjectMultiple);

        List<SubjectAnswerBo> subjectAnswerBos = MultipleSubjectConverter.INSTANCE
                .convertMultipleListToBoAnswerBoList(multipleResult);
        SubjectOptionBo subjectOptionBo = new SubjectOptionBo();
        subjectOptionBo.setOptionList(subjectAnswerBos);
        return subjectOptionBo;
    }
}
